/*
 * FileName：SysOrgCtrl.java 
 * <p>
 * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (1154808491@qq.com)</a>.
 * <p>
 * Licensed under the GNU General Public License, Version 3 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.gnu.org/licenses/gpl-3.0.html
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package com.smart.primary.sys.ctrl;


import java.util.List;

import com.smart.primary.common.common.BaseCtrl;
import com.smart.primary.common.common.Constants;
import com.smart.primary.common.util.AjaxResult;
import com.smart.primary.sys.service.OrgService;
import com.smart.primary.sys.vo.Org;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* 组织机构信息表 Controller层
*
* @author hermit
* @date 2018-02-23 11:02:18
*/
@Controller
@RequestMapping("/sys/org")
public class OrgCtrl extends BaseCtrl {

	@Autowired
    private OrgService sysOrgService;

	/**
	* 获取页面分页数据集合
	*
	* @author hermit
	* @date 2018-02-23 11:02:18
	* @param sysOrg
	* @return
	*/
	@RequestMapping("/list")
	@ResponseBody
	public List<Object> list(Org sysOrg)
		throws Exception{

		List<Object> resList = this.sysOrgService.getOrgListTree(sysOrg);
		return resList;
	}

	/**
	* 数据保存(新增或修改)
	*
	* @author hermit
	* @date 2018-02-23 11:02:18
	* @param sysOrg
	* @return
	*/
	@RequestMapping("/save")
	@ResponseBody
	public AjaxResult save(Org sysOrg)
		throws Exception{

		return this.sysOrgService.saveOrUpdate(sysOrg,request,response);
	}


	/**
	* 数据删除
	*
	* @author hermit
	* @date 2018-02-23 11:02:18
	* @param sysOrg
	* @return
	*/
	@RequestMapping("/delete")
	@ResponseBody
	public AjaxResult delete(Org sysOrg)
		throws Exception{

		/****** STEP_1:判断是否是叶子节点(只有叶子节点可以删除) ***************/
		sysOrg = this.sysOrgService.queryObject(sysOrg);
		// 是否是叶子节点：0否 1是
		int isLeaf = sysOrg.getIsLeaf();
		if(0 == isLeaf){
			return AjaxResult.failure("请先删除子节点");
		}

		// 是否系统参数：0否 1是
		int isSystem = sysOrg.getIsSystem();
		if(1 == isSystem){
			return AjaxResult.failure("系统数据禁止删除");
		}

		/****** STEP_2:执行删除操作 ***************/
		int i = sysOrgService.delete(sysOrg.getOrgNo());

		if(i > 0){
			return AjaxResult.deleteSuccess();
		}else{
			return AjaxResult.failure(Constants.ERROR_TIPS_DELETE);
		}
	}

	/**
	 * 数据删除
	 *
	 * @author hermit
	 * @date 2018-02-23 11:02:18
	 * @param sysOrg
	 * @return
	 */
	@RequestMapping("/getByOrgNo")
	@ResponseBody
	public AjaxResult getByOrgNo(Org sysOrg)
			throws Exception{

		sysOrg = sysOrgService.queryObject(sysOrg);
		return AjaxResult.success(sysOrg);
	}

	/**
	 * 根据orgNo获取对于的子列表.
	 *
	 * @author hermit
	 * @date 2018-02-23 11:02:18
	 * @param sysOrg
	 * @return
	 */
	@RequestMapping("/getListByPid")
	@ResponseBody
	public AjaxResult getListByPid(Org sysOrg)
			throws Exception{

		/************* STEP_1:如果有列表数据 则显示列表数据 ******************/
		List<Org> list = sysOrgService.getListByPid(sysOrg);
		return getResult(sysOrg,list);
	}

    @RequestMapping("/getListByPid2")
    @ResponseBody
    public List<Org> getListByPid2(Org sysOrg)
        throws Exception{

        /************* STEP_1:如果有列表数据 则显示列表数据 ******************/
        List<Org> list = sysOrgService.getListByPid(sysOrg);
        return list;
    }

}
